package com.wangsh.forward.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.forward.outer.service.IClientOuterService;
import com.wangsh.forward.outer.service.IServerOuterService;
import com.wangsh.forward.outer.service.impl.ClientOuterServiceImpl;
import com.wangsh.forward.outer.service.impl.ServerOuterServiceImpl;
import com.wangsh.webforward.common.pojo.ApiResponse;
import com.wangsh.webforward.common.pojo.ApiResponseEnum;
import com.wangsh.webforward.common.servlet.BaseServlet;
import com.wangsh.webforward.common.util.ConstatFinalUtil;

/**
 * 	Api接口调用
 * 
 * @author TeaBig
 */
public class ApiOuterServlet extends BaseServlet
{
	private static final long serialVersionUID = 1L;
	/* 创建一个ServiceOuter的实现类 */
	private IServerOuterService serverOuterService;
	private IClientOuterService clientOuterService;
	
	@Override
	public void initObject()
	{
		if(serverOuterService == null)
		{
			serverOuterService = new ServerOuterServiceImpl() ; 
		}
		if(clientOuterService == null)
		{
			clientOuterService = new ClientOuterServiceImpl() ; 
		}
	}
	
	/**
	 * Servlet的核心方法,不管是Post还是get都要执行此方法
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 调用父类的Service方法 */
		super.service(request, response);
		
		/* 接收一个method参数 */
		String method = request.getParameter("method");
		if("server".equalsIgnoreCase(method))
		{
			this.server(request,response);
		}else if("client".equalsIgnoreCase(method))
		{
			this.client(request,response);
		}
	}
	
	/**
	 * 客户端服务(有以下描述)
	 * 	~请求获取任务列表,
	 * 	~循环所有的任务列表,拿到请求信息,
	 * 	~相当于多次请求,多线程请求网络;
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void client(HttpServletRequest request, HttpServletResponse response) throws IOException 
	{
		ConstatFinalUtil.SYS_LOGGER.info("==client===");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String url = request.getParameter("url");
		String json = request.getParameter("json") ; 
		
		JSONObject reqJSON = new JSONObject() ;
		if(!"".equalsIgnoreCase(json))
		{
			/* 传了上行参数,解析 */
			try
			{
				reqJSON = (JSONObject) JSON.parse(json);
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("解析上行参数失败了,上行参数:{}",json,e);
				/* 上传的json数据格式不正确 */
				Map<String, String> infoMap = new HashMap<String, String>();
				infoMap.put("info", e.toString());
				//code=10
				apiResponse.setCode(ApiResponseEnum.INFO_JSON_FORMAT_ERROR.getStatus());
				//上传的json数据格式不正确,信息:${info},("info",e.toString) 
				apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
				String responseStr =  apiResponse.toJSON().toJSONString() ; 
				/* 输出数据 */
				this.printOut(response, responseStr);
				return ; 
			}
		}
		
		try
		{
			if(url == null)
			{
				url = "";
			}
			
			apiResponse = this.clientOuterService.clientTaskListService(url,reqJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("处理接口报错了,json:{}",url,e);
			//resultJSON.put("code", "13");
			Map<String, String> infoMap = new HashMap<String, String>();
			infoMap.put("info", e.toString());
			apiResponse.setCode(ApiResponseEnum.INFO_JSON_SERVER_ERROR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		String responseStr =  apiResponse.toJSON().toJSONString() ; 
		/* 输出数据 */
		this.printOut(response, responseStr);
	}

	/**
	 * 转发请求-服务端;(有以下描述)
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void server(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==server===");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String json = request.getParameter("json");
		/* 处理参数 */
		if(json == null)
		{
			json = "" ; 
		}
		
		if(!"".equalsIgnoreCase(json))
		{
			JSONObject reqJSON = new JSONObject() ;
			/* 传了上行参数,解析 */
			try
			{
				reqJSON = (JSONObject) JSON.parse(json);
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("解析上行参数失败了,上行参数:{}",json,e);
				/* 上传的json数据格式不正确 */
				Map<String, String> infoMap = new HashMap<String, String>();
				infoMap.put("info", e.toString());
				//code=10
				apiResponse.setCode(ApiResponseEnum.INFO_JSON_FORMAT_ERROR.getStatus());
				//上传的json数据格式不正确,信息:${info},("info",e.toString) 
				apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
				String responseStr =  apiResponse.toJSON().toJSONString() ; 
				/* 输出数据 */
				this.printOut(response, responseStr);
				return ; 
			}
			
			try
			{
				/* 第一个协议版本 */
				apiResponse = this.serverOuterService.apiVersionService(reqJSON);
				/* 版本号不正确 */
				//apiResponse.setCode(ApiResponseEnum.INFO_JSON_VERSION_ERROR.getStatus());
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("处理接口报错了,json:{}",json,e);
				//resultJSON.put("code", "13");
				Map<String, String> infoMap = new HashMap<String, String>();
				infoMap.put("info", e.toString());
				apiResponse.setCode(ApiResponseEnum.INFO_JSON_SERVER_ERROR.getStatus());
				apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
				String responseStr =  apiResponse.toJSON().toJSONString() ; 
				/* 输出数据 */
				this.printOut(response, responseStr);
				return ; 
			}
		}else
		{
			/* 请上传上行参数 */
			//resultJSON.put("code", "9");
			apiResponse.setCode(ApiResponseEnum.INFO_UPLOAD_DATA_EMPTY.getStatus());
		}
		String responseStr =  apiResponse.toJSON().toJSONString() ; 
		/* 输出数据 */
		this.printOut(response, responseStr);
		return ; 
	}

}
